﻿using System;
using System.Runtime.Caching;
using ThirdPrize.Models;

namespace ThirdPrize.Classes
{
    /// <summary>
    /// caches the tag cloud for each topic (ie, music, games, etc).
    /// </summary>
    public class TagCloudCache 
    {
        private ObjectCache Cache { get { return MemoryCache.Default; } }

        public TagCloudModel Get(string topic)
        {
            return Cache[topic] as TagCloudModel;
        }

        public void Set(string topic, TagCloudModel data, int cacheTime)
        {
            CacheItemPolicy policy = new CacheItemPolicy();
            policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime);

            if (Cache[topic] != null)
                Invalidate(topic);
            Cache.Add(new CacheItem(topic, data), policy);
        }

        public bool IsSet(string topic)
        {
            return (Cache[topic] != null);
        }

        public void Invalidate(string topic)
        {
            Cache.Remove(topic);
        }
    }
}